<template>
	<view>
		<Header :isBack="true">
			<block slot="content">{{title}}</block>
		</Header>

		<view class="search-box">
			<view class="search">
				<input class="search-input" type="text" placeholder="石材普工" v-model="keyword">
				<image class="search-img" src="@/static/images/sousuo.png" alt="" @tap="getResult"></image>
			</view>
		</view>

		<FilterDropdown :menuTop="menuTop" :filterData="filterData" :defaultSelected="defaultSelected"
			:updateMenuName="true" dataFormat="Object"></FilterDropdown>

		<!-- 占位 -->
		<view class="place"></view>

		<!-- 空白页 -->
		<empty v-if="isLoaded === false" :top="182"></empty>

		<view class="" v-show="isLoaded">
			<view class="list" v-if="jobData.length > 0">
				<view class="list-item" v-for="(item,index) in jobData" :key="index">
					<view class="img-wrapper" @tap="checkDetail(item.uid,item.realname)">
						<view class="img-wrapper-header">
							<image class="pic" :src="item.avatar"></image>
							<image v-if="type == 2" class="type" src="@/static/images/fujin.png">
							</image>
							<image v-if="type == 3 && item.is_new == 1" class="type" src="@/static/images/news.png">
							</image>
							<view class="img-wrapper-center">
								<view class="img-wrapper-center1">
									<text>{{item.realname}}</text><text style="margin-left:13rpx;"> {{item.sex}}</text>
									<text style="margin-left:13rpx;"> {{item.age}}岁</text>
								</view>
								<view class="img-wrapper-center2">
									<image src="/static/images/yirenzheng.png" style="width:97rpx;height:38rpx;">
									</image>
									<text class="img-wrapper-center2-year">
										{{item.work_year}}年
									</text>
								</view>

							</view>
							<image class="img-wrapper-right" src="/static/images/xiangyou.png"
								style="width:12rpx;height:25rpx;"></image>
						</view>
					</view>
					<view class="introduce" @tap="checkDetail(item.uid,item.realname)">
						<view class="work">
							{{item.jobsort}}
						</view>
						<view class="introduce-content">
							{{item.introduction}}
						</view>
					</view>

					<view class="bot">
						<text class="bot-text">{{item.regtime}}</text>
						<view class="bot-img-text">
							<image class="bot-img" src="/static/images/place.png"></image>
							<text class="bot-text" v-if="type == 2">距离：{{item.distance}}km</text>
							<text class="bot-text" v-else>{{item.jobarea}}</text>
						</view>
					</view>
				</view>

			</view>
			<view class="empty" v-else>
				<image src="/static/images/zanwushuju.png" alt="" class="empty-img"></image>
			</view>
		</view>

		<!-- 底部菜单 -->
		<BottomTabbar :selected="0"></BottomTabbar>
	</view>

</template>

<script>
	import BottomTabbar from '@/components/bottom-tabbar/bottom-tabbar.vue';
	import FilterDropdown from '@/components/HM-filterDropdown/HM-filterDropdown.vue';
	import empty from "@/components/empty";
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				title: '',
				indexArr: '',
				valueArr: [],
				// #ifdef APP-PLUS
				menuTop: '250',
				// #endif

				// #ifdef MP-WEIXIN
				menuTop: '250',
				// #endif

				// #ifdef H5
				menuTop: '198',
				// #endif

				loadingText: "正在加载...",
				defaultSelected: [],
				filterData: [],

				jobData: [],
				page: 1,
				keyword: '',
				isLoaded: false,

				type: 1,
			}
		},

		components: {
			// Header,
			BottomTabbar,
			FilterDropdown,
			empty
		},

		computed: {
			...mapState(['hasLogin']),
		},

		// 页面生命周期，下拉刷新后触发
		onPullDownRefresh() {
			//判断是否登录
			if (this.type == 3) {
				let result = this.$api.isLogin();
				if (result === false) {
					return false;
				}
			}
			this.loadData()

			// 停止下拉刷新
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 500)
		},

		//下拉到底获取新的数据
		onReachBottom() {
			this.getListBySearch(this.valueArr)
		},

		onLoad: function(option) {
			this.title = option.title;
			this.type = option.type;
			//判断是否登录
			if (this.type == 3) {
				let result = this.$api.isLogin();
				if (result === false) {
					return false;
				}
			}

			uni.showLoading({
				title: '加载中...'
			});

			this.loadData(option.keyword);
			setTimeout(() => {
				this.isLoaded = true;
				uni.hideLoading();
			}, 600)

			//筛选条件
			uni.$on('confirm', (cdata) => {
				this.keyword = ''
				this.valueArr = cdata.value;

				this.page = 1;
				this.getListBySearch(this.valueArr)
			})

			uni.$on('changeNew', () => {
				if (this.page > 1) {
					--this.page;
				}
				this.getNewJobs();
			})
		},
		methods: {
			async loadData(keyword = '') {

				this.page = 1;
				this.valueArr = [];
				this.keyword = '';

				let dataObj = {};
				if (keyword) {
					this.keyword = keyword;
					dataObj.keyword = keyword;
					this.valueArr.push({
						'keyword': keyword
					});
				}

				this.getAllSearch();
				this.getNewJobs(dataObj);
			},
			//获取搜索条件
			async getAllSearch() {
				let sdata = await this.$myRequest({
					url: '/common/allsearchc'
				})

				let data = sdata.data.data;
				this.filterData = data;
			},
			//关键字搜索
			async getResult() {
				let keyword = this.keyword.trim();
				if (!keyword) {
					this.$api.msg('请输入搜索内容')
					this.keyword = ''
					return false;
				}

				if (this.valueArr.length > 0) {
					this.getAllSearch();
				}

				let dataObj = {
					keyword: keyword
				};

				// this.jobData = [];
				this.page = 1;
				this.valueArr.push({
					'keyword': keyword
				});
				this.getNewJobs(dataObj);
			},

			async getListBySearch(cdata) {
				let dataObj = {};
				if (cdata.length > 0) {

					cdata.forEach(function(item, index) {

						// if (item[1] != null) {
						switch (index) {
							case 0:
								dataObj.district = item[2] ? item[2] : item[1];
								break;
							case 1:

								dataObj.jobsort = item[1];
								break;
							case 2:
								dataObj.work_year = item[0][0];
								break;
							case 3:
								dataObj.sex = item[0][0];
								break;
						}
						// } else if ('keyword' in item) {
						// 	dataObj.keyword = item.keyword;
						// }
					});
				}

				// console.log(dataObj)
				this.getNewJobs(dataObj);

			},

			//工人列表
			async getNewJobs(dataObj = {}) {
				// console.log(this.jobData);
				dataObj.page = this.page;
				dataObj.type = this.type;
				// console.log(dataObj);
				if (this.type == 2) {
					uni.getLocation({
						type: 'gcj02',
						success: (res) => {
							dataObj.lat = res.latitude;
							dataObj.lng = res.longitude;

							this.getNewJobss(dataObj);
						},
						fail: (res) => {
							this.$api.msg('获取地址失败，将导致部分功能不可用')
						}
					});
				} else {
					this.getNewJobss(dataObj);
				}

			},

			async getNewJobss(dataObj = {}) {

				let url = '/user/lists';
				if (this.type == 3) {
					url = '/company/jobsc';
					dataObj.token = uni.getStorageSync('token')
				}
				let wdata = await this.$myRequest({
					url: url,
					data: dataObj,
					methods: 'post'
				})

				let ndata = wdata.data.data;

				if (this.page === 1) {
					this.jobData = ndata;
					if (ndata.length === 0) {
						this.$api.msg('亲！暂无数据！！！')
					} else {
						this.page = this.page + 1;
					}
				} else {
					if (ndata.length === 0) {
						if (this.jobData.length === 0) {
							this.$api.msg('亲！暂无数据！！！')
						} else {
							this.$api.msg('亲！已经到底了！！！')
						}

					} else {
						this.jobData = [...this.jobData, ...ndata];
						this.page = this.page + 1;
					}
				}

				this.jobData = this.jobData.map((item) => {
					if (!item.avatar) {
						item.avatar = '/static/images/touxiang.png'
					}
					return item
				})
			},

			async checkDetail(uid, title) {
				// console.log(uid)
				// console.log(title)
				if (this.hasLogin) {
					//添加浏览记录
					let token = uni.getStorageSync('token')
					let dataObj = {
						token: token,
						personal_uid: uid
					}
					await this.$myRequest({
						url: '/addBrowseWorker',
						data: dataObj,
						methods: 'POST'
					})

					if (this.type == 3) {
						uni.$emit('changeNew')
					}
				}
				uni.navigateTo({
					url: '/pages3/pages/worker-detail/worker-detail?uid=' + uid + '&title=' + title + '详情'
				});
			}
		}
	}
</script>
<style>
	page {
		background-color: #f8f9fc;
		font-size: $font-base;
	}
</style>
<style lang='scss' scoped>
	.search-box {
		// #ifdef APP-PLUS
		padding: 176rpx 30rpx 0;
		// #endif

		// #ifdef MP-WEIXIN
		padding: 176rpx 30rpx 0;
		// #endif

		// #ifdef H5
		padding: 124rpx 30rpx 0;
		// #endif
	}

	.search {
		height: 66rpx;
		line-height: 66rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-radius: 10rpx;
		/* margin-left: 12%; */
		background-color: #fff;
		border: 1px solid #f3f3f6;


		.search-input {
			height: 58rpx;
			font-size: 26rpx;
			padding-left: 29upx;
			background-color: #fff;
			border-radius: 23.5upx;
			width: 72%;
		}

		.search-img {
			float: right;
			width: 110rpx;
			height: 58rpx;
			margin-right: 4rpx;
		}
	}

	.list {
		padding: 17rpx;
		padding-bottom: 120rpx;
		font-size: $font-base;

		.list-item {
			background-color: #fff;
			height: 440rpx;
			border-radius: 10rpx;
			margin-bottom: 15rpx;

			.img-wrapper {
				width: 100%;
				height: 160rpx;
				position: relative;
				padding: 27rpx;
				border-bottom: 1px solid #f3f3f6;

				.img-wrapper-header {
					margin-bottom: 27rpx;
					height: 112rpx;
					position: relative;

					.pic {
						width: 112rpx;
						height: 112rpx;
						border-radius: 50%;
						margin-right: 17rpx;
					}

					.type {
						position: absolute;
						top: -26rpx;
						left: -26rpx;
						width: 101rpx;
						height: 53rpx;
					}

					.img-wrapper-center {
						height: 112rpx;
						display: inline-block;
						position: absolute;
						line-height: 56rpx;
						color: #333333;

						.img-wrapper-center2 {
							display: flex;
							height: 38rpx;

							.img-wrapper-center2-year {
								margin-left: 20rpx;
								padding: 0 12rpx;
								background-color: #3371f0;
								color: #fff;
								border-radius: 10rpx;
								line-height: 38rpx !important;
								height: 38rpx !important;
								font-size: 24rpx;
							}
						}
					}

					.img-wrapper-right {
						position: absolute;
						top: 42rpx;
						right: 0;
						line-height: 56rpx;
						height: 56rpx;
					}
				}
			}

			.introduce {
				height: 204rpx;
				padding: 27rpx;
				font-size: $font-base !important;

				.work {
					display: inline-block;
					background-color: #ebebeb;
					padding: 6rpx 10rpx;
					border-radius: 6rpx;
					font-size: 26rpx;
				}

				.introduce-content {
					margin-top: 14rpx;
					line-height: 48rpx;
					color: #676767;
					overflow: hidden;
					-webkit-line-clamp: 2;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
				}


			}

			.bot {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 78rpx;
				font-size: 28upx;
				color: #676767;
				padding: 0 27rpx;
				width: 100%;
				border-top: 1px solid #f3f3f6;

				.bot-img-text {
					display: flex;
					align-items: center;
					justify-content: space-between;
					max-width: 360rpx;

					.bot-img {
						height: 28rpx;
						width: 26rpx;
						margin-right: 14rpx;

					}

					.bot-text {
						max-width: 320rpx;
						display: inline-block;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
			}

		}
	}

	.datajson {
		z-index: 2;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 250px;
		padding: 0 10px;
		background-color: rgba(255, 255, 255, .8);
		backdrop-filter: blur(15px);
		-webkit-backdrop-filter: blur(15px);
		font-size: 12px;

		.text {
			margin-top: 10px;
		}

		.item {
			width: 15px;
		}
	}

	.place {
		margin-top: 14rpx;
		background-color: #ffffff;
		height: 44px;
	}

	.swiper {
		width: 100%;
		margin-top: 10upx;
		display: flex;
		justify-content: center;

		.swiper-box {
			width: 92%;
			height: 230rpx;

			overflow: hidden;
			border-radius: 15rpx;
			box-shadow: 0upx 8upx 25rpx rgba(0, 0, 0, 0.2);
			//兼容ios，微信小程序
			position: relative;
			z-index: 1;

			swiper {
				width: 100%;
				height: 230rpx;

				swiper-item {
					image {
						width: 100%;
						height: 230rpx;
					}
				}
			}

			.indicator {
				position: absolute;
				bottom: 20upx;
				left: 20upx;
				background-color: rgba(255, 255, 255, 0.4);
				width: 150upx;
				height: 5upx;
				border-radius: 3upx;
				overflow: hidden;
				display: flex;

				.dots {
					width: 0upx;
					background-color: rgba(255, 255, 255, 1);
					transition: all 0.3s ease-out;

					&.on {
						width: (100%/3);
					}
				}
			}
		}
	}

	.empty {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);

		.empty-img {
			width: 182rpx;
			height: 182rpx;
		}
	}
</style>
